#include "student.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void printError() {
  if (errno) {
    printf_s("错误代码：%d\n", errno);
    printf_s("printf打印标准输出:%s\n", strerror(errno));
  }
}

/// @brief
/// @return
Student *newStudent() {
  Student *stu = (Student *)calloc(sizeof(Student), 1);

  printf_s("请输入学号:");
  scanf_s("%d", &(stu->no));
  printf_s("请输入姓名:");
  scanf_s("%s", stu->name);
  while (stu->name[sizeof(stu->name) - 1] != 0) {
    printf_s("姓名越界\n");
    memset(stu->name, 0, sizeof(stu->name));
    
    printf_s("请输入姓名:");
    scanf_s("%s", stu->name);
    printError();
  }
  printf_s("请输入性别:");
  scanf_s("%s", stu->gender);
  while (stu->gender[sizeof(stu->gender) - 1] != 0) {
    printf_s("性别越界\n");
    memset(stu->gender, 0, sizeof(stu->gender));
    printf_s("请输入性别:");
    scanf_s("%s", stu->gender);
    printError();
  }

  printf_s("请输入成绩:");
  scanf_s("%d", &(stu->grade));
  printError();
  printf_s("您输入的信息如下:\n");
  printStudent(stu);

  return stu;
}

/// @brief 获取学生编号
/// @param student 
int getStudentNo(void * student){
  Student *stu = (Student *)student;
  return stu->no;
}

/// @brief 打印
/// @param student
void printStudent(void *student) {
  if (student == NULL) {
    return;
  }
  Student *stu = (Student *)student;
  printf_s("学号:%d, 姓名:%s, 性别:%s, 成绩:%d\n", stu->no, stu->name,
           stu->gender, stu->grade);
}

/// @brief 释放空间
/// @param student
void freeStudent(void *student) {
  Student *stu = (Student *)student;
  printf_s("释放Student,%d\n", stu->no);
  free(student);
}